package com.minyon_web_admin.service;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.minyon_web_admin.model.SysUserBean;
 
@Service 
public class PasswordHelper {
	protected final Logger minYonlogger = (Logger) LoggerFactory
			.getLogger(getClass());

	private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
	@Value("${password.algorithmName}")
	private String algorithmName="md5";
	@Value("${password.hashIterations}")
	private int hashIterations=2;

	public void encryptPassword(SysUserBean user) {

		String salt = randomNumberGenerator.nextBytes().toHex();//76d1c56a9508fc5ec62d0e287b046c2c
		user.setSalt(salt);
		String newPassword = new SimpleHash( algorithmName,  //加密算法  
                user.getPassword(),      //密码  
                ByteSource.Util.bytes(user.getLoginName()+salt),  //salt盐   username + salt  
                hashIterations   //迭代次数  
                ).toHex();  
        user.setPassword(newPassword); //94b1dbf4396344a7134e77f26e0118a6
        minYonlogger.info("当前用户加密信息  密码：" + newPassword+"  盐值:"+salt);
	}
 
}
